#!/bin/bash

###SHELLPACK preamble filebench-install 1.4.9.1
WEB_LOCATION="https://downloads.sourceforge.net/project/filebench/older/filebench-1.4.9.1/filebench-1.4.9.1.tar.gz"
MIRROR_LOCATION="$WEBROOT/filebench"

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd

rm -rf $SHELLPACK_SOURCES/filebench-${VERSION}
###SHELLPACK sources_fetch filebench-${VERSION}/filebench-${VERSION}.tar.gz filebench-$VERSION

###SHELLPACK build_start filebench-$VERSION

FACTOR=$((MEMTOTAL_BYTES/(7500*1048576)+1))
echo Shared region sized by factor $FACTOR
sed -i -e "s/#define\sFILEBENCH_NFILESETENTRIES.*/#define FILEBENCH_NFILESETENTRIES (1024*1024*$FACTOR)/" ipc.h

# Patch benchmark with fix for direct IO
TEMPFILE=`mktemp`
LINECOUNT=`wc -l $0 | awk '{print $1}'`
PATCHSTART=`grep -n "BEGIN PATCH FILE" $0 | tail -1 | awk -F : '{print $1}'`
tail -$(($LINECOUNT-$PATCHSTART)) $0 | grep -v "^###" > $TEMPFILE
cat $TEMPFILE | patch -p1 || exit $SHELLPACK_FAILURE
rm $TEMPFILE

# Remove pthread unlock which never locks
sed -i -e '/Release the read lock, allowing threads to start/d' parser_gram.y
sed -i -e '/pthread_rwlock_unlock/d' parser_gram.y

###SHELLPACK build_configure filebench-${VERSION}
###SHELLPACK make_make_install

exit $SHELLPACK_SUCCESS

=== BEGIN PATCH FILE ===
diff --git a/flowop_library.c b/flowop_library.c
index 634897e..85ac02a 100644
--- a/flowop_library.c
+++ b/flowop_library.c
@@ -491,7 +491,7 @@ flowoplib_iobufsetup(threadflow_t *threadflow, flowop_t *flowop,
 	}
 
 	if (flowoplib_fileattrs(flowop) & FLOW_ATTR_DIRECTIO)
-		*iobufp = (caddr_t)((((unsigned long)(*iobufp)) / 512) * 512);
+		*iobufp = (caddr_t)((((unsigned long)(*iobufp) + 512) / 512) * 512);
 
 	return (FILEBENCH_OK);
 }
